﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.API.Community.V1
{
    using LdCms.EF.DbModels;
    using LdCms.Common.Extension;
    using LdCms.IBLL.Community;
    using LdCms.Web.Services;
    /// <summary>
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("community/column")]
    public class CommunityColumnController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IHttpContextAccessor Accessor;
        private readonly IConfigService ConfigService;
        private readonly IColumnService ColumnService;
        public CommunityColumnController(IBaseApiManager BaseApiManager, IHttpContextAccessor Accessor, IConfigService ConfigService, IColumnService ColumnService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.Accessor = Accessor;
            this.ConfigService = ConfigService;
            this.ColumnService = ColumnService;
        }
        /// <summary>
        /// 获取社区专栏列表
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("list")]
        public IActionResult GetColumnAll(string uuid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid);
                int systemId = InterfaceAccount.SystemID;
                string companyId = InterfaceAccount.CompanyID;
                var lists = ColumnService.GetColumnAll(systemId, companyId, true);
                if (lists == null)
                    return Error(logId, "not column！");
                var data = from m in lists
                           orderby m.Sort.ToInt()
                           select new
                           {
                               id = m.ColumnID,
                               name = m.ColumnName,
                               image = m.ImgSrc.IIF(),
                               title = m.Title.IIF(),
                               keyword = m.Keyword.IIF(),
                               description = m.Description.IIF(),
                               sort = m.Sort.ToInt(),
                               locks = m.IsLock.ToBool()
                           };
                int total = data.Count();
                return Success(logId, "ok", new { total, rows = data });
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }
        /// <summary>
        /// 获取社区专栏明细
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("get")]
        public IActionResult GetColumn(string uuid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid);
                int systemId = InterfaceAccount.SystemID;
                string companyId = InterfaceAccount.CompanyID;
                string columnId = GetQueryString("id");
                var entity = ColumnService.GetColumn(systemId, companyId, columnId);
                if (entity == null)
                    return Error(logId, "not data！");
                var data = new
                {
                    id = entity.ColumnID,
                    name = entity.ColumnName,
                    image = entity.ImgSrc.IIF(),
                    title = entity.Title.IIF(),
                    keyword = entity.Keyword.IIF(),
                    description = entity.Description.IIF(),
                    sort = entity.Sort.ToInt(),
                    locks = entity.IsLock.ToBool()
                };
                return Success(logId, "ok", data);
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }


    }
}